package com.shop.manager.controller;

//import com.shop.manager.entity.Item;
//import com.shop.manager.entity.bean.Cart;
//import com.shop.manager.http.HttpResult;
//import com.shop.manager.http.PageResult;
import com.base.common.entity.Item;
import com.base.common.entity.bean.Cart;
import com.base.common.http.HttpResult;
import com.base.common.http.PageResult;
import com.shop.manager.service.ItemService;
import com.shop.manager.utils.JsonUtils;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

/**
 * @Description 产品增删改查
 */
@RestController
@RequestMapping("/item")
public class ItemController {

    /**
     * @Description 注入service对象
     */
    @Autowired
    private ItemService tbItemService;

    /**
     * @Description 添加商品详情
     * @param item 商品信息对象
     * @return
     */
    @RequestMapping(value = "/add", method = RequestMethod.POST)
    public HttpResult add(@RequestBody(required = false) @ApiParam(value = "Item 对象") Item item){
        try {
            tbItemService.add(item);
            return HttpResult.ok("添加商品详情成功!");
        } catch (Exception e) {
            e.printStackTrace();
        }
        return HttpResult.error("添加商品详情失败!");
    }

    /**
     * @Description 批量删除商品详情
     * @param ids 商品id的数组
     * @return
     */
    @RequestMapping("/delete")
    public HttpResult delete(@RequestBody Long[] ids){
        try {
            tbItemService.deleteByIds(ids);
            //删除成功
            return HttpResult.ok();
        } catch (Exception e) {
            e.printStackTrace();
            //删除异常
            return HttpResult.error();
        }
    }

    /**
     * @Description 修改商品详情
     * @param item 修改的商品对象
     * @return
     */
    @RequestMapping("/update")
    public HttpResult update(@RequestBody(required = false) @ApiParam(value = "Item 对象") Item item){
        try {
            tbItemService.update(item);
            return HttpResult.ok("修改商品详情成功!");
        } catch (Exception e) {
            e.printStackTrace();
        }
        return HttpResult.error("修改商品详情失败!");
    }

    /**
     * @Description 根据ID查询商品详情
     * @param id 商品id
     * @return
     */
    @RequestMapping("/getById")
    public HttpResult getById(@RequestParam("id") Long id){
        return HttpResult.ok(tbItemService.findOne(id));
    }

    /**
     * @Description 条件分页查询
     * @param pageNum 当前页码
     * @param size 当前页条数
     * @param item 查询条件对象
     * @return
     */
    @RequestMapping("/getAll")
    public HttpResult getAll(@RequestParam("pageNum") Integer pageNum, @RequestParam("size") Integer size, @RequestBody(required = false) Item item){
        try {
            //分页查询
            PageResult result = tbItemService.findPage(pageNum, size, item);
            //查询成功
            return HttpResult.ok(result);
        } catch (Exception e) {
            e.printStackTrace();
            //查询异常
            return HttpResult.error("查询商品详情异常");
        }
    }

    /**
     * @Description 分页查询
     * @param page 当前页码
     * @param rows 当前页条数
     * @return
     */
    @RequestMapping(value = "/query", method = RequestMethod.POST)
    public PageResult findAllAttr(@RequestParam("page") Integer page, @RequestParam("rows") Integer rows) {
        PageResult pageResult = tbItemService.findPage(page, rows);
        return pageResult;
    }

    /**
     * @Description 模糊查询商品详情
     * @param item 商品详情的对象
     * @return
     */
    @RequestMapping(value = "/findByWhere", method = RequestMethod.POST)
    public HttpResult findByWhere(@RequestBody(required = false) @ApiParam(value = "Item 对象") Item item) {
        return HttpResult.ok(tbItemService.findByWhere(item));
    }

    /**
     * @Description 设置商品状态，1-正常，2-下架，3-删除
     * @param ids 需要修改状态的商品id数组
     * @param status 状态 '1'、'2'、'3'
     * @return
     */
    @RequestMapping(value = "/updateStatus", method = RequestMethod.POST)
    public HttpResult updateStatus(@RequestBody Long[] ids, @RequestParam("status") String status){
        return HttpResult.ok(tbItemService.updateStatus(ids,status));
    }

    /**
     * @Description 判断是否可以扣减库存，执行扣减库存
     * @param cartListStr
     * @return
     */
    @RequestMapping(value = "/deductionStock", method = RequestMethod.POST)
    public boolean deductionStock(@RequestParam("cartListStr") String cartListStr){

        List<Cart> cartList = JsonUtils.jsonToList(cartListStr, Cart.class);
        boolean flag = tbItemService.deductionStock(cartList);
        return flag;
    }

    @RequestMapping(value = "/getDynamicPage", method = RequestMethod.GET)
    public HttpResult getDynamicPage(@RequestParam("goodsId") Long goodsId){
        try {
            return HttpResult.ok(tbItemService.getDynamicPage(goodsId));
        } catch (Exception e) {
            return HttpResult.error("商品不存在");
        }

    }




}
